<?php

function rpc( $json_request ) {
    global $PARAPARA;

    $req_param = $json_request->param;

    if(!$req_param) {
        return new RPC_Error($json_request->operation, "PARAM", "No params");
    };

    $dbl = $PARAPARA->dblayer;

    $token = $req_param->token;
    $token = str_replace( ' ', '+', $token );
    if( ($token == null) || !strlen($token))
        return new RPC_Error($json_request->operation, "PARAM", "Missing token parameter");

    $stuff = Token::parse( $token );
    if($stuff['status'] != 'ok')
        return new RPC_Error($json_request->operation, "AUTH", "Authentication error 1: token:$token");

    $uname   = $stuff['uname'];
    $pwd_md5 = $stuff['pwd_md5'];

    $account = $dbl->account_fetch_by_screenname( $uname );

    if( !$account )
        return new RPC_Error($json_request->operation, "AUTH", "Authentication error 2");

    if( !$account->check_authentication( $uname, $pwd_md5 ) )
        return new RPC_Error($json_request->operation, "AUTH", "Authentication error pwd_md5=$pwd_md5 == " . $account->pwd_md5 );

    $rpc_retval = 
        array(
            'token'       => Token::generate($uname, $pwd_md5, 3600 ),
            'screen_name' => $account->screen_name,
            'karma'       => $account->karma
            );

    return new RPC_Response( $json_request->operation, $rpc_retval );
}

return rpc( $json_request );

?>